﻿using ProjectScheduling.models;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;

namespace ProjectScheduling.daos
{
    public class ProjectDao : BaseDao<Projects>
    {
        public readonly static ProjectDao Base = new();
        public int AddProject(Projects projects)
        {
            var projectId = Add(projects);
            if (projects.Staffs != null && projects.Staffs.Count > 0)
            {
                projects.Staffs.ForEach(item =>
                {
                    (int id, string name) = item;
                    ProjectStaffDao.Base.Add(new ProjectStaff
                    {
                        ProjectId = id,
                        UserId = id,
                        UserName = name
                    });
                });
            }
            return projectId;
        }
        public int UpdateProject(Projects projects)
        {
            var result = Update(projects, it => new
            {
                it.Address,
                it.Name,
                it.Description,
                it.FromDate,
                it.ToDate
            });
            ProjectStaffDao.Base.Delete(it => it.ProjectId == projects.Id);
            if (projects.Staffs != null && projects.Staffs.Count > 0)
            {
                projects.Staffs.ForEach(item =>
                {
                    (int id, string name) = item;
                    ProjectStaffDao.Base.Add(new ProjectStaff
                    {
                        ProjectId = id,
                        UserId = id,
                        UserName = name
                    });
                });
            }
            return result;
        }
        public int DeleteProject(Projects projects)
        {
            Delete(projects.Id);
            ProjectStaffDao.Base.Delete(it => it.ProjectId == projects.Id);
            return 1;
        }
        public new List<Projects> FindAll(Expression<Func<Projects, bool>> func)
        {
            var list = Db.Queryable<Projects>().Where(func).OrderByDescending(it => it.Id).Includes(it => it.StaffsList).ToList();
            return list;
        }
        public new PageData<Projects> FindAll(int page, int row, Expression<Func<Projects, bool>> func)
        {
            int total = 0;
            return new PageData<Projects>
            {
                Items = Db.Queryable<Projects>().Where(func).OrderByDescending(it => it.Id).Includes(it => it.ProjectType).Includes(it => it.Sales).Includes(it => it.StaffsList).Includes(it => it.Creator).ToPageList(page, row, ref total),
                Total = total
            };
        }
        public PageData<Projects> FindAll1(int page, int row, Expression<Func<Projects, bool>> func)
        {
            List<int> arr = [4, 2, 8];
            var list = new List<(Enum_ProjectState state, int id)> { (Enum_ProjectState.实施中, 1), (Enum_ProjectState.已排期, 2), (Enum_ProjectState.已完结, 3), (Enum_ProjectState.挂起, 4), (Enum_ProjectState.待排期, 5) };
            int total = 0;
            return new PageData<Projects>
            {
                Items = Db.Queryable<Projects>().
                OrderBy(it => it.State).OrderByDescending(it => it.FromDate)

                .Where(func).Includes(it => it.ProjectType).Includes(it => it.Sales).Includes(it => it.StaffsList).Includes(it => it.Creator).Includes(it => it.PreparedDatas).ToPageList(page, row, ref total),
                Total = total
            };
        }
        public new Projects Find(Expression<Func<Projects, bool>> func)
        {
            return Db.Queryable<Projects>().Includes(it => it.ProjectType).Includes(it => it.Sales).Includes(it => it.StaffsList).First(func);
        }
        public new Projects Find(int id) => Find(it => it.Id == id);
        

    }
}
